package com.hksj.Intelligentmanagement.auth.controller;

import com.hksj.Intelligentmanagement.auth.service.LoginService;
import com.hksj.Intelligentmanagement.auth.vo.LoginInfoVo;
import com.hksj.Intelligentmanagement.common.Rest;
import com.hksj.Intelligentmanagement.common.Result;
import com.hksj.Intelligentmanagement.dto.LoginDTO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.security.Principal;

@Api(tags = "登录管理")
@RestController
@RequestMapping("/api/login")
public class LoginController {

    @Autowired
    private LoginService loginServcie;

    @ApiOperation(value = "用户登录")
    @PostMapping("/doLogin")
    public Result doLogin(@RequestBody @Valid LoginDTO dto) {
        return loginServcie.login(dto);
    }

    @ApiOperation(value = "用户登出")
    @GetMapping("/doLogout")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = Result.class),
    })
    public Result logout() {
        return loginServcie.logout();
    }

    @ApiOperation(value = "踢人下线")
    @GetMapping("/kick")
    public Result kick(
        @ApiParam(value = "用户id",required = true) @RequestParam String userId) {
        return loginServcie.kick(userId);
    }

    @ApiOperation(value = "测试接口")
    @GetMapping("/hellow")
    //当前用户是否有test权限，
//    @PreAuthorize("hasAuthority('system')")
    @PreAuthorize("hasAnyRole('ceo')")
    public String hellow() {
        return "hellow";
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("/getUserInfo")
    public Rest<LoginInfoVo> getUserInfo(HttpServletRequest request, Principal principal){
        LoginInfoVo loginInfoVo = loginServcie.getUserInfo(request,principal);
        return Rest.ok(loginInfoVo);
    }
}
 